package com.F09.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

import com.dao.DataOperations;
import com.dbtools.Dispose;
import com.exception.DeleteException;
import com.exception.InsertException;
import com.exception.SelectException;
import com.exception.UpdateException;
import com.info.FunctionInfo;
import com.util.CommonTools;
/**
 * 
 * @file FunctionDAO.java
 * @author Liushimin
 * @Modify 2008-10-16
 * @version 1.0
 */
public class FunctionDAO extends DataOperations{
	/**
	 */
	Logger log = Logger.getLogger(FunctionDAO.class);
	public FunctionDAO(){
		
	}
    public FunctionDAO(Connection connection){
        super(connection);
    }
    public List selectByObject() throws SelectException{
    	List list = new ArrayList();
    	StringBuffer sql = new StringBuffer();   
    	//sql.append("SELECT FUNCTIONID,CODE,NAME,PARENTID,'('||FUN_GETBH('FUNCTIONID',PARENTID)||')'||FUN_GETMC('FUNCTIONID',PARENTID) PARENTNAME, URL, NOTES  FROM T_S_FUNCTION ");
    	//sql.append(" where FUNCTIONID!='0' ORDER BY PARENTID,XH ASC ");
    	sql.append("SELECT FUNCTIONID,CODE,NAME,PARENTID,'('+adyh.FUN_GETBH('FUNCTIONID',PARENTID)+')'+adyh.FUN_GETMC('FUNCTIONID',PARENTID) PARENTNAME, URL, NOTES  FROM T_S_FUNCTION ");
    	sql.append(" where FUNCTIONID!='0' ORDER BY PARENTID,XH ASC ");
    	ResultSet rest = null;
    	try {
    		log.info(sql.toString());
    		rest = this.executeQuery(sql.toString());
			while(rest.next()){
				Map map = new HashMap();
				map.put("FUNCTIONID", CommonTools.null2String(rest.getString("FUNCTIONID")));
				map.put("CODE",  CommonTools.null2String(rest.getString("CODE")));
				map.put("NAME", CommonTools.null2String(rest.getString("NAME")));
				map.put("PARENTID", CommonTools.null2String(rest.getString("PARENTID")));
				map.put("PARENTNAME", CommonTools.null2String(rest.getString("PARENTNAME")));
				map.put("URL", CommonTools.null2String(rest.getString("URL")));
				map.put("NOTES", CommonTools.null2String(rest.getString("NOTES")));
				list.add(map);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			Dispose.dispose(rest);
		}
    	return list;
    }
        public List selectById(String key) throws SelectException{
    	List list = new ArrayList();
    	StringBuffer sql = new StringBuffer();    	
    	//sql.append("SELECT FUNCTIONID, CODE, NAME, PARENTID,'(' ||FUN_GETBH('FUNCTIONID',PARENTID)|| ')' || FUN_GETMC('FUNCTIONID',PARENTID) PARENTNAME, URL, NOTES,XH  FROM T_S_FUNCTION ");
    	//sql.append(" where FUNCTIONID = '").append(key).append("'" );
    	
    	sql.append("SELECT A.FUNCTIONID, A.CODE, A.NAME, A.PARENTID,'('+A.CODE+')'+B.NAME PARENTNAME, A.URL, A.NOTES,A.XH  FROM T_S_FUNCTION A,T_S_FUNCTION B");
    	sql.append(" where A.FUNCTIONID = '").append(key).append("' and A.PARENTID=B.FUNCTIONID" );
    	ResultSet rest = null;
    	try {
    		rest = this.executeQuery(sql.toString());
			while(rest.next()){
				Map map = new HashMap();
				map.put("FUNCTIONID", CommonTools.null2String(rest.getString("FUNCTIONID")));
				map.put("CODE",  CommonTools.null2String(rest.getString("CODE")));
				map.put("NAME", CommonTools.null2String(rest.getString("NAME")));
				map.put("PARENTID", CommonTools.null2String(rest.getString("PARENTID")));
				map.put("PARENTNAME", CommonTools.null2String(rest.getString("PARENTNAME")));
				map.put("URL", CommonTools.null2String(rest.getString("URL")));
				map.put("NOTES", CommonTools.null2String(rest.getString("NOTES")));
				map.put("XH", CommonTools.null2String(rest.getString("XH")));
				list.add(map);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			Dispose.dispose(rest);
		}
		return list;
    }
    public List selectByCode(Object obj) throws SelectException{
    	List list = new ArrayList();
    	FunctionInfo info = (FunctionInfo) obj;
    	StringBuffer sql = new StringBuffer();    	
    	sql.append("SELECT FUNCTIONID, CODE, NAME, PARENTID, URL, NOTES  FROM T_S_FUNCTION ");
    	sql.append(" where CODE = '").append(info.getCode()).append("'" );
    	if(!CommonTools.isNullString(info.getFunctionId())){
    		sql.append(" AND FUNCTIONID != '"+info.getFunctionId()+"' ");
    	}
    	System.out.println("SELECT = "+sql.toString());
    	ResultSet rest = null;
    	try {
    		rest = this.executeQuery(sql.toString());
			while(rest.next()){
				Map map = new HashMap();
				map.put("FUNCTIONID", CommonTools.null2String(rest.getString("FUNCTIONID")));
				list.add(map);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			Dispose.dispose(rest);
		}
    	return list;
    }
    
    public boolean insert(Object obj) throws InsertException {
        boolean flag = true;
        FunctionInfo info = (FunctionInfo) obj;
        String sql = new String();
        List sqlList = new ArrayList();
        sql= " INSERT INTO  T_S_FUNCTION (CODE, NAME, PARENTID, URL, NOTES,XH ) ";
        sql+=" VALUES('"+info.getCode()+"','"+info.getName()+"','"+info.getParentId()+"','"+info.getUrl()+"','"+info.getNote()+"','"+info.getXh()+"')";
    	System.out.println("INSERT = "+sql.toString());
        sqlList.add(sql);
        try {
        	flag = this.executeUpdate(sqlList,true);
        }
        catch (SQLException e) {
          throw new InsertException(e.getMessage());
        }
        return flag;
      }
    
    public boolean update(Object obj) throws UpdateException {
        boolean flag = true;
        FunctionInfo info = (FunctionInfo) obj;
        String sql = new String();
        List sqlList = new ArrayList();
        sql= " update T_S_FUNCTION set ";
        sql+=" CODE='"+info.getCode()+"',NAME='"+info.getName()+"',URL='"+info.getUrl()+"',PARENTID='"+info.getParentId()+"',NOTES='"+info.getNote()+"',XH='"+info.getXh()+"'";
        sql+=" where FUNCTIONID ='"+info.getFunctionId()+"'";
    	System.out.println("update= "+sql.toString());
        sqlList.add(sql);
        try {
        	flag = this.executeUpdate(sqlList,true);
        }
        catch (SQLException e) {
          throw new UpdateException(e.getMessage());
        }
        return flag;
      }
    public void deleteByKey(String key) throws DeleteException {
        List list = new ArrayList();
		key = key.replaceAll(",","','");
		String roleFunlSql = new StringBuffer().append("delete from T_S_ROLE_FUNCTION where FUNCTIONID in('").append(key).append("')").toString();
		String funSql = new StringBuffer().append("delete from T_S_FUNCTION  WHERE FUNCTIONID in ('").append(key).append("')").toString();
    	System.out.println("roleFunlSql = "+roleFunlSql);
    	System.out.println("funSql"+funSql);
		list.add(roleFunlSql);
		list.add(funSql);
		try {
			super.executeUpdate(list,true);
		} catch (SQLException e) {
			throw new DeleteException(e.getMessage());
		}
    }
}
